<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Rt Class
 *
 * Get subscriber account details
 *
 * @package     CodeIgniter
 * @subpackage  Libraries
 * @category    Accounts
 * @author      Ian Odiaman | Fluxion, Inc
 * @link
 */
class Rt {

    /**
     * Constructor
     *
     * @access public
     */
    function __construct()
    {
        log_message('debug', "Rt Class Initialized");
	}
    
    // --------------------------------------------------------------------
    
    /**
     * Send SMS RT
     *
     * @access   public
     * @param    String
     * @return   Object
     */
    public function sendsms($to = '', $msg = '')
	{
		$this->CI =& get_instance();
		$this->CI->config->load('config_hessian');
		
		// Get Hessian library
		require_once $this->CI->config->item('hessian_client');
		require_once $this->CI->config->item('hessian_options');
		
		$from = $this->CI->config->item('rt_from');
		
		// Certificate path for dev only
		$CERT_PATH = $this->CI->config->item('rt_cert_path');
		
		// Private key to be used
		$private_key = openssl_pkey_get_private( $this->CI->config->item('rt_private_key') );

		// API URL
		$url = $this->CI->config->item('rt_url');
		
			// Validate if pasok sa mobile number format
			if(preg_match('/^09[0-9]{9}$/', $to)) {
			
				// Check if message has content
				$msg = trim($msg);
				if(!empty($msg)) {
					
					// Create SMS Hessian call
					$opts = new HessianOptions();
					$opts->typeMap[ 'RTPayload' ] = 'com.seer.seerium.common.RTPayload';
					$opts->typeMap[ 'RTResult' ] = 'com.seer.seerium.common.RTResult';
					$opts->version = 1;
					$opts->saveRaw=true;
					$opts->transport = "curl";

					// COMMENT OUT FOR DEV, SEND CERTIFICATE
					$opts->transportOptions = array(
						CURLOPT_SSL_VERIFYHOST => 2,
						CURLOPT_SSL_VERIFYPEER => FALSE,
						CURLOPT_CAINFO => "$CERT_PATH" );

					$proxy = new HessianClient( $url, $opts );
				
					$payload = new RTPayload();
					$payload->CSP_Txid = $this->_generateTxid( $to );
					$payload->CP_Id= "brewcon";
					$payload->CP_UserId= "brewcon";
					$payload->CP_Password= "brewcon";
					$payload->SMS_MsgTxt = $msg;
					$payload->SMS_Msgdata= "";
					$payload->SMS_SourceAddr= $from;
					$payload->SUB_R_Mobtel = $to;
					$payload->keyPrefix= 'rudyvas';
					
					// SSbean
					$sharedSecret = "";
					$plaintext = $payload->SUB_R_Mobtel . $payload->SMS_MsgTxt;
					if ( strlen( $plaintext ) ) {
						$plaintext = substr( $plaintext, 0, 245 );
					}
					openssl_private_encrypt( $plaintext, $sharedSecret, $private_key );
					$payload->sharedSecret = base64_encode( $sharedSecret ) ;
				
					// Send SMS
					$result = $proxy->sendSMS( $payload );
					log_message('debug', "Message " . $msg . " to " . $to);
					return true;
					
				} else {
					log_message('error', "Invalid message");
					return false;
				}

			} else {
				// Invalid mobile number, redirect back to home
				log_message('error', "Invalid mobile number");
				return false;
			}
		
    }
	
		
	/**
     * Create verification code
     *
     * @access   public
     * @return   String
     */
    public function generateVerificationCode()
	{
		// Generate 6 number string code
		$code = mt_rand(100000, 999999);
		//$code = 987654; // dummy for stress test		
		return $code;
    }
	
	function _generateTxid( $mobile ) {
		date_default_timezone_set("Asia/Manila");
		return "globe" . $mobile. date("YmdHi");
	}
}

class RTPayload {
    var $CSP_Txid;
    var $CP_Id;
    var $CP_UserId;
    var $CP_Password;
    var $SMS_MsgTxt;
    var $SMS_Msgdata;
    var $SMS_SourceAddr;
    var $SUB_R_Mobtel;
    var $keyPrefix;
    var $sharedSecret;
}

class RTResult {
    var $status;
    var $details;
}